using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The FeeTypeCodeTableDB class is responsible for interacting with the database to retrieve and store information
    /// about FeeTypeCodeTable objects.
    /// </summary>
    public class FeeTypeCodeTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of FeeTypeCodeTable from the underlying datasource.
        /// </summary>
        /// <param name="feeTypeCode">The unique FeeTypeCode of the FeeTypeCodeTable in the database.</param>
        /// <returns>An FeeTypeCodeTable when the FeeTypeCode was found in the database, or null otherwise.</returns>
        public static FeeTypeCodeTable GetItem(short feeTypeCode)
        {
            FeeTypeCodeTable myFeeTypeCodeTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocFeeTypeCodeTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@feeTypeCode", feeTypeCode);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            myFeeTypeCodeTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return myFeeTypeCodeTable;
            }
        }

        /// <summary>
        /// Returns a list with FeeTypeCodeTable objects.
        /// </summary>
        /// <returns>A generics List with the FeeTypeCodeTable objects.</returns>
        public static FeeTypeCodeTableList GetList()
        {
            FeeTypeCodeTableList tempList = new FeeTypeCodeTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocFeeTypeCodeTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a FeeTypeCodeTable in the database.
        /// </summary>
        /// <param name="myFeeTypeCodeTable">The FeeTypeCodeTable instance to save.</param>
        /// <returns>The new FeeTypeCode if the FeeTypeCodeTable is new in the database or the existing FeeTypeCode when an item was updated.</returns>
        public static short Save(FeeTypeCodeTable myFeeTypeCodeTable)
        {
            short result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocFeeTypeCodeTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (myFeeTypeCodeTable.FeeTypeCode == -1)
                    {
                        myCommand.Parameters.AddWithValue("@feeTypeCode", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@feeTypeCode", myFeeTypeCodeTable.FeeTypeCode);
                    }
                    if (string.IsNullOrEmpty(myFeeTypeCodeTable.ShortDescription))
                    {
                        myCommand.Parameters.AddWithValue("@shortDescription", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@shortDescription", myFeeTypeCodeTable.ShortDescription);
                    }
                    if (string.IsNullOrEmpty(myFeeTypeCodeTable.Description))
                    {
                        myCommand.Parameters.AddWithValue("@description", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@description", myFeeTypeCodeTable.Description);
                    }


                    myCommand.Parameters.AddWithValue("@status", myFeeTypeCodeTable.Status);

                    if (myFeeTypeCodeTable.CreatedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@createdDateTime", myFeeTypeCodeTable.CreatedDateTime);
                    }
                    if (myFeeTypeCodeTable.ModifiedDateTime == null)
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@modifiedDateTime", myFeeTypeCodeTable.ModifiedDateTime);
                    }

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt16(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a FeeTypeCodeTable from the database.
        /// </summary>
        /// <param name="feeTypeCode">The FeeTypeCode of the FeeTypeCodeTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(short feeTypeCode)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocFeeTypeCodeTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@feeTypeCode", feeTypeCode);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the FeeTypeCodeTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static FeeTypeCodeTable FillDataRecord(IDataRecord myDataRecord)
        {
            FeeTypeCodeTable myFeeTypeCodeTable = new FeeTypeCodeTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FeeTypeCode")))
            {
                myFeeTypeCodeTable.FeeTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("FeeTypeCode"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ShortDescription")))
            {
                myFeeTypeCodeTable.ShortDescription = myDataRecord.GetString(myDataRecord.GetOrdinal("ShortDescription"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Description")))
            {
                myFeeTypeCodeTable.Description = myDataRecord.GetString(myDataRecord.GetOrdinal("Description"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                myFeeTypeCodeTable.Status = myDataRecord.GetBoolean(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                myFeeTypeCodeTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                myFeeTypeCodeTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return myFeeTypeCodeTable;
        }
    }

}
